using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Runtime.InteropServices;
using System.Windows.Forms.Design;
using Microsoft.VisualStudio.Shell;
using AntEater.ComponentModel;
using AntEater.ComponentModel.Building;
using AntEater.Runtime;

namespace AntEater.Integration.OptionPages
{
    /// <summary>
    /// Provides the option page for the general settings.
    /// </summary>
    [Guid(Guids.GeneralOptionPageId)]
    [CLSCompliant(false)]
    public class GeneralOptionPage : DialogPage
    {
        private const string Delemiter = ";";

        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralOptionPage"/> class.
        /// </summary>
        public GeneralOptionPage()
            : base() {
            _buildFileExtensions = new BuildFileExtensionCollection();
            this.BuildFileExtensionsString = Strings.BuildFileExtensionsDefault;

            _buildEnginePath = RuntimeEnvironment.GetRuntimeDirectory();
            _buildLoggingVerbosity = BuildingLoggerVerbosity.Minimal;
        }

        private string _buildEnginePath;

        /// <summary>
        /// Gets or sets the build engine path.
        /// </summary>
        /// <value>The build engine path.</value>
        [Category(Strings.BuildFileSettingsCategory)]
        [DisplayName(Strings.BuildEnginePathName)]
        [Description(Strings.BuildEnginePathDescription)]
        [Editor(typeof(FolderNameEditor), typeof(UITypeEditor))]
        public string BuildEnginePath {
            get { return _buildEnginePath; }
            set { _buildEnginePath = value; }
        }

        private BuildFileExtensionCollection _buildFileExtensions;

        /// <summary>
        /// Gets the build file extensions.
        /// </summary>
        /// <value>The build file extensions.</value>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public BuildFileExtensionCollection BuildFileExtensions {
            get { return _buildFileExtensions; }
        }

        /// <summary>
        /// Gets or sets the build file extensions as string.
        /// </summary>
        /// <value>The build file extensions as string.</value>
        [Category(Strings.BuildFileSettingsCategory)]
        [DisplayName(Strings.BuildFileExtensionsName)]
        [Description(Strings.BuildFileExtensionsDescription)]
        public string BuildFileExtensionsString {
            get {
                string[] extensions = new string[_buildFileExtensions.Count];
                _buildFileExtensions.CopyTo(extensions, 0);
                return string.Join(GeneralOptionPage.Delemiter, extensions);
            }
            set {
                if (value == null) { throw new ArgumentNullException("value"); }

                _buildFileExtensions.Clear();
                foreach (string extension in value.Split(new string[] { GeneralOptionPage.Delemiter }, StringSplitOptions.RemoveEmptyEntries)) {
                    _buildFileExtensions.Add(extension);
                }
            }
        }

        private BuildingLoggerVerbosity _buildLoggingVerbosity;

        /// <summary>
        /// Gets or sets the build logging verbosity.
        /// </summary>
        /// <value>The build logging verbosity.</value>
        [Category(Strings.BuildFileSettingsCategory)]
        [DisplayName(Strings.BuildingVerbosityName)]
        [Description(Strings.BuildingVerbosityDescription)]
        public BuildingLoggerVerbosity BuildLoggingVerbosity {
            get { return _buildLoggingVerbosity; }
            set { _buildLoggingVerbosity = value; }
        }

        private bool _showImportedTargets;

        /// <summary>
        /// Gets or sets a value indicating whether the imported targets should be displayed.
        /// </summary>
        /// <value><c>true</c> if the imported targets should be displayed; otherwise, <c>false</c>.</value>
        [Category(Strings.TargetExecutionSettingsCategory)]
        [DisplayName(Strings.ShowImportedTargetsName)]
        [Description(Strings.ShowImportedTargetsDescription)]
        public bool ShowImportedTargets {
            get { return _showImportedTargets; }
            set { _showImportedTargets = value; }
        }


        /// <summary>
        /// Raises the <see cref="E:Apply"/> event.
        /// </summary>
        /// <param name="e">The <see cref="Microsoft.VisualStudio.Shell.DialogPage.PageApplyEventArgs"/> instance containing the event data.</param>
        protected override void OnApply(PageApplyEventArgs e) {

            // Set options on runtime instance
            if (e.ApplyBehavior == ApplyKind.Apply) {
                Options options = AntEaterRuntime.Instance.Options;
                options.BuildEnginePath = _buildEnginePath;
                options.ShowImportedTargets = _showImportedTargets;
                options.BuildLoggingVerbosity = _buildLoggingVerbosity;

                options.BuildFileExtensions.Clear();
                options.BuildFileExtensions.AddRange(_buildFileExtensions);
            }

            base.OnApply(e);
        }
    }
}
